import SwiftUI
import AVKit
import Foundation

struct PeapixWallpaper: Codable {
    let title: String
    let copyright: String
    let date: String
    let pageUrl: String
    let fullUrl: URL
    let thumbUrl: URL
    let imageUrl: URL
}

@available(macOS 12.0, *)
struct WallpaperPreview: View {
    let wallpaper: PeapixWallpaper?
    let isVideo: Bool
    @State private var showError = false
    @State private var errorMessage = ""
    @State private var isHovered = false
    @State private var player: AVPlayer?
    
    private func setupPlayer() {
        guard let wallpaper = wallpaper else { return }
        player = AVPlayer(url: wallpaper.fullUrl)
        player?.pause()
    }
    
    
    private var videoPlayerView: some View {
        GeometryReader { geometry in
            Group {
                if let player = player {
                    VideoPlayer(player: player)
                        .frame(width: geometry.size.width, height: geometry.size.height)
                        .aspectRatio(contentMode: .fill)
                        .cornerRadius(8)
                        .clipped()
                        .onAppear {
                            player.pause()
                        }
                        .onChange(of: isHovered) { hovered in
                            if hovered {
                                player.play()
                            } else {
                                player.pause()
                            }
                        }
                        .onDisappear {
                            player.pause()
                        }
                } else {
                    Text("播放器初始化失败")
                        .foregroundColor(.white)
                        .padding(8)
                        .background(Color.black.opacity(0.7))
                        .cornerRadius(8)
                }
            }
            .overlay(
                Group {
                    if player?.status == .failed {
                        Text("视频加载失败")
                            .foregroundColor(.white)
                            .padding(8)
                            .background(Color.black.opacity(0.7))
                            .cornerRadius(8)
                    } else if player?.status == .readyToPlay {
                        EmptyView()
                    } else {
                        ProgressView()
                    }
                }
            )
        }
        .frame(height: 150)
    }
    
    var body: some View {
        VStack {
            if let wallpaper = wallpaper {
                if isVideo {
                    videoPlayerView
                        .onAppear {
                            setupPlayer()
                        }
                        .onDisappear {
                            player?.pause()
                            player = nil
                        }
                } else {
                    VStack(spacing: 8) {
                        AsyncImage(url: wallpaper.thumbUrl) { phase in
                            if let image = phase.image {
                                image
                                    .resizable()
                                    .aspectRatio(contentMode: .fill)
                            } else if phase.error != nil {
                                Text("图片加载失败")
                            } else {
                                ProgressView()
                            }
                        }
                        VStack(alignment: .leading) {
                            Text(wallpaper.title)
                                .font(.headline)
                            Text(wallpaper.copyright)
                                .font(.caption)
                                .foregroundColor(.secondary)
                            Text("发布日期: \(wallpaper.date)")
                                .font(.caption)
                        }
                    }
                    .frame(height: 150)
                    .cornerRadius(8)
                    .clipped()
                }
            } else {
                Rectangle()
                    .fill(Color.gray.opacity(0.2))
                    .frame(height: 150)
                    .cornerRadius(8)
                    .overlay(
                        Text("选择壁纸以预览")
                            .foregroundColor(.gray)
                    )
            }
        }
        .alert("错误", isPresented: $showError) {
            Button("确定", role: .cancel) {}
        } message: {
            Text(errorMessage)
        }
    }
}